package dao;

import dbc.DBI;
import dbc.MyConn;
import vo.Good;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewDaolmpl implements INewDao {
    public static void main(String[] args) {
        try {
            System.out.println(new NewDaolmpl().getAllNew().size());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    @Override
    public List<Good> getAllNew() throws SQLException {
       List<Good>list=new ArrayList<>();
       //打开数据库
        DBI dbi = new MyConn();
        Connection conn = dbi.getConn();
         //发送sql语句
        PreparedStatement ps=conn.prepareStatement("select * from  news");
        ResultSet rs = ps.executeQuery();//发送查询语句
        //发送增删改查 ps.executeUpdate()
        while (rs.next()){
            Good good = new Good();
            good.setId(rs.getInt(1));
            good.setName(rs.getString(4));
            good.setPrice(rs.getFloat(3));
            good.setPic(rs.getString(2));
            good.setNum(rs.getInt(5));
            good.setInfo(rs.getNString(3));
            list.add(good);
        }
        //释放资源，关闭数据库
        dbi.closeAll(null,ps,rs);
        dbi.closeConn();
        return list;
    }

    @Override
    public Good getOneNewByID(int id) throws SQLException {
        return null;
    }

    @Override
    public boolean deleteByid(int id) throws SQLException {
        return false;
    }
}
